package org.lq.er.view.scoreManege;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Exam_information;
import org.lq.er.entity.Score_info;
import org.lq.er.entity.Student;
import org.lq.er.entity.Subject;
import org.lq.er.service.Exam_informationService;
import org.lq.er.service.Score_infoService;
import org.lq.er.service.StudentService;
import org.lq.er.service.SubjectService;
import org.lq.er.service.impl.Exam_informationServiceImpl;
import org.lq.er.service.impl.Score_infoServiceImpl;
import org.lq.er.service.impl.StudentServiceImpl;
import org.lq.er.service.impl.SubjectServiceImpl;
import org.lq.er.view.login.V_login;


import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class V_ScoreUpdate extends JFrame {

	private JPanel contentPanel;
	private JTextField txt_id;
	private JTextField txt_name;
	private JTextField txt_class;
	private JTextField txt_score;
	private static String[] str;
	Score_infoService sis = new Score_infoServiceImpl();
	SubjectService sus = new SubjectServiceImpl();
	StudentService sts = new StudentServiceImpl();
	Exam_informationService eis = new Exam_informationServiceImpl();
	private JRadioButton btnque;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_ScoreUpdate frame = new V_ScoreUpdate(str);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_ScoreUpdate(String[] str) {
		setTitle("修改学生成绩");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 416, 442);
		setLocationRelativeTo(null);
		contentPanel = new JPanel();
		contentPanel.setBackground(Color.white);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPanel);
		contentPanel.setLayout(null);

		JLabel lblNewLabel = new JLabel("身份证号：");
		lblNewLabel.setBounds(40, 30, 67, 22);
		contentPanel.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("姓名：");
		lblNewLabel_1.setBounds(40, 88, 67, 22);
		contentPanel.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("班级：");
		lblNewLabel_2.setBounds(40, 146, 67, 22);
		contentPanel.add(lblNewLabel_2);


		JRadioButton btnque = new JRadioButton("缺考");
		if(str[4].equals("是")) {
			btnque.setSelected(true);
		}else {
			btnque.setSelected(false);
		}
		btnque.setBounds(73, 224, 127, 23);

		contentPanel.add(btnque);

		JLabel lblNewLabel_2_1 = new JLabel("成绩：");
		lblNewLabel_2_1.setBounds(40, 272, 67, 22);
		contentPanel.add(lblNewLabel_2_1);

		txt_id = new JTextField();
		txt_id.setBounds(133, 31, 199, 21);
		txt_id.setText(str[0]);
		contentPanel.add(txt_id);
		txt_id.setColumns(10);

		txt_name = new JTextField();
		txt_name.setColumns(10);
		txt_name.setText(str[1]);
		txt_name.setBounds(133, 89, 199, 21);
		contentPanel.add(txt_name);

		txt_class = new JTextField();
		txt_class.setColumns(10);
		txt_class.setText(str[2]);
		txt_class.setBounds(133, 147, 199, 21);
		contentPanel.add(txt_class);

		txt_score = new JTextField();
		txt_score.setText(str[5]);
		txt_score.setColumns(10);

		txt_score.setBounds(133, 273, 199, 21);
		contentPanel.add(txt_score);


		JLabel lblNewLabel_3 = new JLabel("科目");
		lblNewLabel_3.setBounds(40, 192, 58, 15);
		contentPanel.add(lblNewLabel_3);

		List<Subject> list = sus.findAll();
		String [] arr = new String[list.size()];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = list.get(i).getSubjectName();
		}

		JComboBox txt_subject = new JComboBox();
		txt_subject.setModel(new DefaultComboBoxModel(arr));
		txt_subject.setSelectedItem(str[3]);
		txt_subject.setBounds(133, 188, 199, 23);
		contentPanel.add(txt_subject);


		JButton btnNewButton = new JButton("提交");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id = txt_id.getText().trim();
				String name = txt_name.getText().trim();
				String classes = txt_class.getText().trim();
				int score = Integer.parseInt(txt_score.getText().trim());
				Student stu = sts.getByCard_id(id);

				Exam_information info = (Exam_information) eis.getByStu_Id(id);
				String subname = txt_subject.getSelectedItem().toString().trim();
				Score_info si = new Score_info();
				si.setStu_id(id);
				Subject sub = sus.getBySubjectName(subname);
				si.setSubject_id(sub.getId());
				if(btnque.isSelected()) {
					si.setStatus(0);
					si.setScore(0);
				}else {
					si.setStatus(1);
					si.setScore(score);
				}
				if(sis.update(si)) {
					JOptionPane.showMessageDialog(V_ScoreUpdate.this, "修改成功","提示",JOptionPane.INFORMATION_MESSAGE);
					dispose();
				}else {
					JOptionPane.showMessageDialog(V_ScoreUpdate.this, "修改失败，请检查","提示",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton.setBounds(97, 344, 121, 51);
		contentPanel.add(btnNewButton);
	}
	public void reset() {
		txt_id.setText("");
		txt_class.setText("");
		txt_name.setText("");
		txt_score.setText("");
		btnque.setSelected(false);
	}
}
